home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_185 / examples / apack.asm next >
Assembly Source File  |  1992-05-06  |  8KB  |  250 lines

  1.  
  2. **************************************************************** 
  3. *  Copyright 1988 by CREATIVE FOCUS.  This code is freely 
  4. *  distributable as long as this notice is retained and no 
  5. *  other conditions are imposed upon its redistribution. 
  6. *  APACK.ASM --  
  7. *  A fully compatible replacement for Electronic Arts' PACKER.C 
  8. *  routine.  Converts data according to the IFF ILBM cmpByteRun1 
  9. *  compression protocol: 
  10. *     control bytes: 
  11. *        n =  0.. 127:   followed by n+1 bytes of data; 
  12. *        n = -1..-127:   followed by byte to be repeated -n+1 times; 
  13. *        n =     -128:   don't do no nada. 
  14. *     calling format: 
  15. *        long PackRow(from, too, amt) 
  16. *           char **from, /* pointer to source data pointer */ 
  17. *                **too;  /* pointer to destination data pointer */ 
  18. *           long amt;    /* number of bytes to compress */ 
  19. *        return(number of bytes written to destination); 
  20. *     effects: 
  21. *         *from = *from + amt, and *too = *too + return; 
  22. *         return is "smart," that is, not greater than 
  23. *         MaxPackedSize = amt + ((amt+127) >> 7). 
  24. *     By commenting out CHECK (below) you disable checking for runs 
  25. *     exceeding 128 bytes.  That CHECK is not needed if you are sure 
  26. *     the amt to be compressed is always 128 or less. 
  27. *  !!! DISCLAIMER !!!  You use this code entirely at your own 
  28. *  risk.  I don't warrantee its fitness for any purpose.  I 
  29. *  can't even guarantee the accuracy of anything I've said 
  30. *  about it, though I've tried my damndest to get it right. 
  31. *  I may, in fact, be completely out of my tiny little mind :-). 
  32. *  That being said, I can be reached for questions, comments, 
  33. *  or concerns at: 
  34. *        Dr. Gerald Hull 
  35. *        CREATIVE FOCUS 
  36. *        12 White Street 
  37. *        Binghamton, N.Y.  13901 
  38. *        (607) 648-4082 
  39. *        bix:    ghull 
  40. *        PLink:  DRJERRY 
  41. *************************************************************** 
  42.  
  43.       xdef  _PackRow 
  44.  
  45. PT    equr  a0                -> beginning of replicate run (if any) 
  46. IX    equr  a1                -> end+1 of input line 
  47. IP    equr  a2                -> beginning of literal run (if any) 
  48. IQ    equr  a3                -> end+1 of lit and/or rep run (if any) 
  49. OP    equr  a4                -> end+1 of output line current pos 
  50. FP    equr  a6                frame pointer 
  51. SP    equr  a7                stack pointer 
  52.  
  53. RT    equr  d0                return value 
  54. MX    equr  d1                check for maximum run = MAX 
  55. AM    equr  d2                amount 
  56. CH    equr  d3                character 
  57.  
  58. REGS  reg   AM/CH/IP/IQ/OP 
  59.  
  60. FRM   equ   8                 input line address 
  61. TOO   equ   12                output line address 
  62. AMT   equ   16                length of input line 
  63.  
  64. MAX   equ   128               maximum encodable output run 
  65. * CHECK equ   1                 turns on maximum row checking 
  66.  
  67.  
  68. _PackRow 
  69.  
  70.  
  71. ***************     CASE 0:   GRAB PARAMS & INITIALIZE 
  72. CAS0 
  73.       link     FP,#0 
  74.       movem.l  REGS,-(SP) 
  75.       movea.l  FRM(FP),IP 
  76.       movea.l  (IP),IP        IP = *from 
  77.       movea.l  IP,IQ          IQ = IP 
  78.       movea.l  IQ,IX 
  79.       adda.l   AMT(FP),IX     IX = IP + amt 
  80.       movea.l  TOO(FP),OP 
  81.       movea.l  (OP),OP        OP = *too 
  82.  
  83.  
  84. ***************     CASE 1:   LITERAL RUN 
  85. CAS1 
  86.       movea.l  IQ,PT          adjust PT (no replicates yet!) 
  87.       move.b   (IQ)+,CH       grab character 
  88.       cmpa.l   IQ,IX          if input is finished 
  89.       beq.s    CAS5              branch to case 5 
  90.  
  91.       ifd      CHECK 
  92.       move.l   IQ,MX 
  93.       sub.l    IP,MX 
  94.       cmpi     #MAX,MX        if run has reached MAX 
  95.       beq.s    CAS6              branch to case 6 
  96.       endc 
  97.  
  98.       cmp.b    (IQ),CH        if next character != CH 
  99.       bne.s    CAS1              stay in case 1 
  100.  
  101. *                             else fall into case 2 
  102.  
  103.  
  104. ***************     CASE 2:   AT LEAST 2 BYTE REPEAT 
  105. CAS2 
  106.       move.b   (IQ)+,CH       grab character 
  107.       cmpa.l   IQ,IX          if input is finished 
  108.       beq.s    CAS7              branch to case 7 
  109.  
  110.       ifd      CHECK 
  111.       move.l   IQ,MX 
  112.       sub.l    IP,MX 
  113.       cmpi     #MAX,MX        if run has reached MAX 
  114.       beq.s    CAS6              branch to case 6 
  115.       endc 
  116.  
  117.       cmp.b    (IQ),CH        if next character != CH 
  118.       bne.s    CAS1              branch to case 1 
  119.  
  120. *                             else fall into case 3 
  121.  
  122.  
  123. ***************     CASE 3:   REPLICATE RUN 
  124. CAS3 
  125.       move.b   (IQ)+,CH       grab character 
  126.       cmpa.l   IQ,IX          if input is finished 
  127.       beq.s    CAS7              branch to case 7 
  128.  
  129.       ifd      CHECK 
  130.       move.l   IQ,MX 
  131.       sub.l    PT,MX 
  132.       cmpi     #MAX,MX        if run has reached MAX 
  133.       beq.s    CAS4              branch to case 4 
  134.       endc 
  135.  
  136.       cmp.b    (IQ),CH        if next character = CH 
  137.       beq.s    CAS3              stay in case 3 
  138.  
  139. *                             else fall into case 4 
  140.  
  141.  
  142. ***************     CASE 4:   LIT AND/OR REP DUMP & CONTINUE 
  143. CAS4 
  144.       move.l   PT,AM 
  145.       sub.l    IP,AM          AM = PT - IP 
  146. *                             if no literal run 
  147.       beq.s    C41               branch to replicate run 
  148.  
  149.       subq     #1,AM          AM = AM - 1 
  150.       move.b   AM,(OP)+       output literal control byte 
  151.  
  152. C40   move.b   (IP)+,(OP)+    output literal run 
  153.       dbra     AM,C40 
  154.  
  155. C41   move.l   PT,AM 
  156.       sub.l    IQ,AM          AM = PT - IQ (negative result!) 
  157.       addq     #1,AM          AM = AM + 1 
  158.       move.b   AM,(OP)+       output replicate control byte 
  159.       move.b   CH,(OP)+       output repeated character 
  160.       movea.l  IQ,IP          reset IP 
  161.       bra.s    CAS1           branch to case 1 (not done) 
  162.  
  163.  
  164. ***************     CASE 5:   LITERAL DUMP & QUIT 
  165. CAS5 
  166.       move.l   IQ,AM 
  167.       sub.l    IP,AM          AM = IQ - IP (positive result > 0) 
  168.       subq     #1,AM          AM = AM - 1 
  169.       move.b   AM,(OP)+       output literal control byte 
  170.  
  171. C50   move.b   (IP)+,(OP)+    output literal run 
  172.       dbra     AM,C50 
  173.  
  174.       bra.s    CAS8           branch to case 8 (done) 
  175.  
  176.  
  177.       ifd      CHECK 
  178.  
  179. ***************     CASE 6:   LITERAL DUMP & CONTINUE 
  180. CAS6 
  181.       move.l   IQ,AM 
  182.       sub.l    IP,AM          AM = IQ - IP (positive result > 0) 
  183.       subq     #1,AM          AM = AM - 1 
  184.       move.b   AM,(OP)+       output literal control byte 
  185.  
  186. C60   move.b   (IP)+,(OP)+    output literal run 
  187.       dbra     AM,C60 
  188.  
  189.       bra      CAS1           branch to case 1 (not done) 
  190.  
  191.       endc 
  192.  
  193.  
  194. ***************     CASE 7:   LIT AND/OR REP DUMP & FINISH 
  195. CAS7 
  196.       move.l   PT,AM 
  197.       sub.l    IP,AM          AM = PT - IP (positive result > 0) 
  198. *                             if no literal run 
  199.       beq.s    C71               branch to replicate run 
  200.  
  201.       subq     #1,AM          AM = AM - 1 
  202.       move.b   AM,(OP)+       output literal control byte 
  203.  
  204. C70   move.b   (IP)+,(OP)+    output literal run 
  205.       dbra     AM,C70 
  206.  
  207. C71   move.l   PT,AM 
  208.       sub.l    IQ,AM          AM = PT - IQ (negative result) 
  209.       addq     #1,AM          AM = AM + 1 
  210.       move.b   AM,(OP)+       output replicate control byte 
  211.       move.b   CH,(OP)+       output repeated character 
  212.  
  213. *                             fall into case 8 
  214.  
  215.  
  216. ***************     CASE 8:   ADJUST PARAMS & RETURN VALUE 
  217. CAS8 
  218.       movea.l  FRM(FP),PT     PT = **from 
  219.       move.l   IQ,(PT)        *from = *from + amt 
  220.       movea.l  TOO(FP),PT     PT = **too 
  221.  
  222.       move.l   OP,RT 
  223.       sub.l    (PT),RT       return = OP - *too  
  224.  
  225.       move.l   OP,(PT)       *too = *too + return 
  226.       movem.l  (SP)+,REGS 
  227.       UNLK     FP 
  228.       rts 
  229.  
  230.       end
  231.  
  232.  
  233.